public class Test {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();

        for(int i = 0;i < s.length();i++) {
            char ch = s.charAt(i);

            if(isfull(ch)) {
                stack.push(ch);
            } else {
                if(stack.empty()) {
                    return false;
                }
                if(stack.peek() == '(' && ch == ')' ||
                        stack.peek() == '{' && ch == '}' ||
                        stack.peek() == '[' && ch == ']' ) {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(stack.empty()) {
            return true;
        }
        return false;
    }

    public boolean isfull(char ch) {
        if(ch == '(' || ch == '[' || ch == '{') {
            return true;
        }
        return false;
    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();

        for(int i = 0;i < s.length();i++) {
            char ch = s.charAt(i);

            if(isfull(ch)) {
                stack.push(ch);
            } else {
                if(stack.empty()) {
                    return false;
                }
                if(stack.peek() == '(' && ch == ')' ||
                        stack.peek() == '{' && ch == '}' ||
                        stack.peek() == '[' && ch == ']' ) {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(stack.empty()) {
            return true;
        }
        return false;
    }

    public boolean isfull(char ch) {
        if(ch == '(' || ch == '[' || ch == '{') {
            return true;
        }
        return false;
    }
}
